package top.hitotright.es;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class ESClient {

    private String indexName = "testindex";

    public static void main(String[] arg){
        ESClient zClient = new ESClient();
        try {
            zClient.init();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

    private void init() throws Exception
    {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("user", "123456"));

        RestClientBuilder builder = RestClient.builder(
                new HttpHost("localhost", 9200))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);}});
        RestHighLevelClient client = new RestHighLevelClient(builder);

        SearchRequest searchRequest = new SearchRequest(this.indexName);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // hit 返回值(bool 查询返回条数)
//        searchSourceBuilder.size(0);
//        searchSourceBuilder.from(0);

        // 准确计数
        searchSourceBuilder.trackTotalHits(true);
        // 超时时间60s
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        // 绑定查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // status字段为301或302
        boolQueryBuilder.must(QueryBuilders.termsQuery("status.keyword", new String[]{"301","302"}));
        // args字段包含786754748671257
        boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("args","786754748671257"));
        // 时间大于等于2020-05-21 00:00:00，小于2020-05-22 00:00:00
        boolQueryBuilder.must(QueryBuilders.rangeQuery("@timestamp").gte(new Date()).lt(new Date()));
        // 绑定bool query
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        // 打印dsl语句
        System.out.println("dsl is："+searchRequest.source());
        // 发起请求并接收响应
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse.toString());
        // close
        client.close();
    }
}
